<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
if($_RQ['op']!='main'){
	cfc('core')->checklogin();
}
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['banner']=$basic['image'];
$return['basic']=$basic;
$return['account']=$_SESSION['account'];
if($_RQ['op']=='main'){
	if(!$_RQ['id']){
		exi('没有找到该作品，请重新选择！','error');
	}
	if($_RQ['isdownsao']){
		$this->session('isdownsao',$_RQ['isdownsao']);
	}
	$opus=pdo_get('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$pindex = max(1, 0);
	$psize = 1;
	$next=pdo_getslice('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'isshow'=>1,'id >'=>$opus['id']),array($pindex,$psize),$total,'','','id asc');
	$return['nextid']=$next[0]['id'];
	if(!$return['nextid']){
		$next=pdo_getslice('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'isshow'=>1),array($pindex,$psize),$total,'','','id asc');
		$return['nextid']=$next[0]['id'];
	}
	$return['nexturl']=custom_url('jy_vote/o_detail//id='.$return['nextid'],true);
	$on=pdo_getslice('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'isshow'=>1,'id <'=>$opus['id']),array($pindex,$psize),$total,'','','id desc');
	$return['onid']=$on[0]['id'];
	if(!$return['onid']){
		$on=pdo_getslice('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'isshow'=>1),array($pindex,$psize),$total,'','','id desc');
		$return['onid']=$on[0]['id'];
	}
	$return['onurl']=custom_url('jy_vote/o_detail//id='.$return['onid'],true);
	//活动
	$reply=pdo_get('jy_vote_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$opus['rid']));
	//分类
	$group=pdo_get('jy_vote_groups',array('uniacid'=>$_SESSION['uniacid'],'id'=>$reply['groupid']));
	$user = pdo_get('jy_vote_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$opus['mid']));
	if(($user['isvip'] !=1 || $user['viptime'] < time()) && $basic['isfee_hongbao']){
		$user['ishongbao'] =0;
	}else{
		$user['ishongbao'] =1;
	}
	$user['avatar']=$user['avatar']?tomedia($user['avatar']):'';
	if($opus['mid'] == $_SESSION['uid']){
		$return['isme']=1;
	}else{
		$return['isme']=0;
	}
	$install = check_permission('jy_poster');
	$return['install']=$install?1:0;
	if(($opus['isshow'] !=1 || $opus['status'] !=1) && $return['isme']==0){
		exi('该作品不公开，或者在审核！','error');
	}
	//是否带商品
	$return['goods']=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$opus['goodsid']),array('id','title','image','price','total','active_plugin','active_id'));
	$user['vipstatus']=$user['viptime']>time()?1:2;
	//是否关注
	$return['isfollow']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');
	//添加一次浏览次数
	pdo_update('jy_vote_opus',array('look +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	if($_RQ['fromuser']){
		pdo_update('jy_vote_opus',array('sharenums +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}
	//算票数/赞数
	$votenums=pdo_count('jy_vote_votelog',array('oid'=>$opus['id'],'uniacid'=>$_SESSION['uniacid']));
	$jifen=pdo_getsum('jy_vote_sendlplog',array('oid'=>$opus['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>1),'jifen');
	if($group['type']==1){
		$opus['likes']=$opus['likes']+$jifen+$opus['baselikenums'];
	}else{
		$opus['votenums']=$votenums+$jifen+$opus['basevotenums'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 3;
	$voteusers=pdo_getslice('jy_vote_votelog',array('oid'=>$opus['id'],'uniacid'=>$_SESSION['uniacid']),array($pindex,$psize),$total,$fields,'','id desc');
	foreach($voteusers as &$v){
		$v['avatar']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'avatar');
	}
	if(count($voteusers)==0){
		$voteusers[]=array('avatar'=>'../core/resource/images/gw-user.png');
		$voteusers[]=array('avatar'=>'../core/resource/images/gw-user.png');
		$voteusers[]=array('avatar'=>'../core/resource/images/gw-user.png');
	}elseif(count($voteusers)==1){
		$voteusers[]=array('avatar'=>'../core/resource/images/gw-user.png');
		$voteusers[]=array('avatar'=>'../core/resource/images/gw-user.png');
	}elseif(count($voteusers)==2){
		$voteusers[]=array('avatar'=>'../core/resource/images/gw-user.png');
	}
	$likeusers=pdo_getslice('jy_vote_looklist',array('oid'=>$opus['id'],'uniacid'=>$_SESSION['uniacid']),array($pindex,$psize),$total,$fields,'','id desc');
	foreach($likeusers as &$lv){
		$lv['avatar']=pdo_getcolumn('core_members',array('id'=>$lv['mid']),'avatar');
	}
	if(count($likeusers)==0){
		$likeusers[]=array('avatar'=>'../core/resource/images/gw-user.png');
		$likeusers[]=array('avatar'=>'../core/resource/images/gw-user.png');
		$likeusers[]=array('avatar'=>'../core/resource/images/gw-user.png');
	}elseif(count($likeusers)==1){
		$likeusers[]=array('avatar'=>'../core/resource/images/gw-user.png');
		$likeusers[]=array('avatar'=>'../core/resource/images/gw-user.png');
	}elseif(count($likeusers)==2){
		$likeusers[]=array('avatar'=>'../core/resource/images/gw-user.png');
	}
	//进入作品详情，记录访问者浏览信息
	$islook=pdo_get('jy_vote_looklist',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$_RQ['id']));
	if(empty($islook)){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'oid'=>$_RQ['id'],
			'mid'=>$_SESSION['uid'],
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_vote_looklist',$insert);
	}else{
		pdo_update('jy_vote_looklist',array('createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$_RQ['id']));
	}
	$opus['thumb']=tomedia($opus['thumb']);
	$opus['createtime']=date('Y-m-d H:i:s',$opus['createtime']);
	$opus['number']=pdo_count('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'rid'=>$opus['rid'],'id <='=>$opus['id']));
	$opus['paiming']=pdo_count('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'rid'=>$opus['rid'],'votenums <='=>$opus['votenums']));
	$opus_images=json_decode($opus['opus_images'],true);
	$this->session('id',$_RQ['id']);
	$liping=pdo_getall('jy_vote_liping',array('uniacid'=>$_SESSION['uniacid'],'rid'=>$opus['rid']));
	$return['liping']=$liping;
	$return['voteusers']=$voteusers;
	$return['likeusers']=$likeusers;
	$return['user']=$user;
	$return['opus']=$opus;
	$return['reply']=$reply;
	$return['group']=$group;
	$return['islike']=$islook['islike'];
	$return['isfans']=pdo_getcolumn('jy_vote_userlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$opus['mid']),'isfollow');
	$return['opus_images']=$opus_images;
	$return['sharedata']=array(
		'title'=>$opus['title'],
		'desc'=>$opus['description'],
		'link'=>custom_url('jy_vote/o_detail//id='.$_RQ['id'].'&fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>ATTACHMENT_URL.$opus_images[0],
	);
	$return['post']=array('nickname'=>pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'nickname'));
	$return['isshoucang']=pdo_getcolumn('jy_vote_shoucang',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$_RQ['id']),'isshoucang');
	$return['list']=array();
	$return['item']=array('video'=>$opus['video']);
	$return['is_nolive']=1;
	$return['msgnum']=pdo_count('core_account_message',array('aid'=>$opus['id'],'uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_vote'));	
	$return['params']=array('aid'=>$opus['id']);
	set_html('title','作品详情');
	exi($return);
}
if($_RQ['op']=='follow'){
	$id = $this->session('id');
	if(!$id){
		exi('没有找到该作品，请重新选择！','error');
	}

	$opus=pdo_get('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($opus['mid'] == $_SESSION['uid']){
		exi('自己不能关注自己！','error');
	}
	$follow=pdo_get('jy_vote_userlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$opus['mid']));
	if($follow){
		if($follow['isfollow'] !=1){
			pdo_update('jy_vote_userlog',array('isfollow'=>1,'createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$opus['mid']));
			exi('关注成功');
		}else{
			pdo_update('jy_vote_userlog',array('isfollow'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$opus['mid']));
			exi('已取消关注');
		}
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'userid'=>$opus['mid'],
			'mid'=>$_SESSION['uid'],
			'isfollow'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_vote_userlog',$insert);
	}
	exi('关注成功');
}
if($_RQ['op']=='shoucang'){
	$id = $this->session('id');
	if(!$id){
		exi('没有找到该作品，请重新选择！','error');
	}
	$opus=pdo_get('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$shoucang=pdo_get('jy_vote_shoucang',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id));
	if($shoucang){
		if($shoucang['isshoucang'] !=1){
			pdo_update('jy_vote_shoucang',array('isshoucang'=>1,'createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id));
			exi('收藏成功');
		}else{
			pdo_update('jy_vote_shoucang',array('isshoucang'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id));
		   exi('取消收藏');
		}
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'oid'=>$id,
			'mid'=>$_SESSION['uid'],
			'isshoucang'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_vote_shoucang',$insert);
	}
	exi('收藏成功');
}
if($_RQ['op']=='like'){
	$id = $this->session('id');
	$postdata=get_postdata();
	if(!$id){
		exi('没有找到该作品，请重新选择！','error');
	}
	$looklist=pdo_get('jy_vote_looklist',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id));
	if($looklist){
		if($looklist['islike'] !=1){
			pdo_update('jy_vote_opus',array('likes +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			pdo_update('jy_vote_looklist',array('islike'=>1),array('oid'=>$id,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
			$return['success']=1;
			exi($return);
			
		}else{
			exi('不能取消点赞','error');
			//pdo_update('jy_vote_opus',array('like -='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			//pdo_update('jy_vote_looklist',array('islike'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id));
		}
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'oid'=>$id,
			'mid'=>$_SESSION['uid'],
			'islike'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_vote_looklist',$insert);
		$return['success']=1;
		exi($return);
	}
	
}
if($_RQ['op']=='setmsg'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
	$postdata=get_postdata();
	$data=array(
		'aid'=>intval($postdata['id']),
		'content'=>trim($postdata['msg']),
		'username'=>$member['nickname'],
		'uid'=>$_SESSION['uid'],
		'uniacid'=>$_SESSION['uniacid'],
		'plugin_name'=>'jy_vote',
		'createtime'=>time(),
	);
	pdo_insert('core_account_message',$data);
	$return=array('msg'=>'评论成功！','avatar'=>$member['avatar']);
	exi($return);
}
if($_RQ['op']=='setvote'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
	$postdata=get_postdata();
	$oid=intval($postdata['id']);
	if(!$oid){
		$oid=$this->session('id');
	}
	if(!$oid){
		exi('未正确获取作品id','error');
	}
	$opus = pdo_get('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$oid));
	$reply = pdo_get('jy_vote_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$opus['rid']));
	if($reply['is_yanzheng'] && !check_image_code(trim($postdata['code']))){
		exi('验证码错误','error');
	}
	if($reply['endtime']<time()){
			exi('该活动已结束！','error');
	}
	if($reply['starttime']>time()){
			exi('该活动还没开始！','error');
	}
	//投票最少作品数
	$opus_sum=pdo_count('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'rid'=>$opus['rid']));
	if($opus_sum<$reply['minpeople']){
		exi('作品数量未达到投票最小作品数，暂无法投票','error');
	}
	//用户已投票数量(包含未审核)
	$vote_sum=pdo_count('jy_vote_votelog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$oid));
	if($vote_sum>=$reply['everyonevote'] && $reply['everyonevote']>0){
		exi('每人最多投'.$reply['everyonevote'].'票，您已投完！','error');
	}
	//日投票
	//获得当日凌晨的时间戳
	$today = strtotime(date("Y-m-d"),time());
	$end = $today+60*60*24;
	$dayvote_sum=pdo_count('jy_vote_votelog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime >'=>$today));
	if($dayvote_sum>=$reply['dairyvote'] && $reply['dairyvote']>0){
		exi('每人每日最多投'.$reply['dairyvote'].'票，您已投完！','error');
	}
	//每日给同一作品投票
	$dayuservote_sum=pdo_count('jy_vote_votelog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime >'=>$today,'oid'=>$oid));
	if($dayuservote_sum>=$reply['everyoneuser'] && $reply['everyoneuser']>0){
		exi('同一作品每人每日最多投'.$reply['everyoneuser'].'票，您已投完！','error');
	}
	//时间段数量限制
	if($reply['timenei'] && $reply['timenei_num']){
		$timeneitime=TIMESTAMP-$reply['timenei']*60;
		$timenei_num=pdo_count('jy_vote_votelog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$oid,'createtime >'=>$timeneitime),'id');
		if($timenei_num>=$reply['timenei_num']){
			exi('该作品'.$reply['timenei'].'分钟内，投票数量已达上限！等会再来吧！','error');
		}
	}
	$isdownsao=$this->session('isdownsao');
	$data=array(
		'mid'=>$_SESSION['uid'],
		'oid'=>$oid,
		'uniacid'=>$_SESSION['uniacid'],
		'is_shenhe'=>1,
		'isline'=>$isdownsao?1:0,
		'user_ip'=>CLIENT_IP,
		'createtime'=>time(),
	);
	pdo_insert('jy_vote_votelog',$data);
	$return['logid']=pdo_insertid();
	//积分奖励：
	//投票赠送
	if($reply['vote_type']>0 && $reply['votezs']){
		cfc('core')->member_credit($_SESSION['uid'],'credit'.$reply['vote_type'],$reply['votezs'],1,'jy_vote','投票赠送');
	}
	if($reply['vote_type']>1 && $reply['vote_couponid']){
		$this->model('coupon')->getcoupon($reply['vote_couponid']);
	}
	$rand=rand(1,100) ;
	if($rand>$reply['tanchu_rate']){
		$istanchu=0;
	}else{
		$istanchu=1;
	}
	if($reply['vote_codetype']){
		if($reply['vote_codetype']==1 && $isdownsao){
			$return['getcode']=1;
		}
		if($reply['vote_codetype']==2 && !$isdownsao){
			$return['getcode']=1;
		}
		if($reply['vote_codetype']==3){
			$return['getcode']=1;
		}
	}
	$return['istanchu']=$reply['hb_draw']?$istanchu:0;
	exi($return);
}
if($_RQ['op']=='getcode'){
	$logid = $_RQ['logid'];
	$rid = $_RQ['rid'];
	$votelog = pdo_get('jy_vote_votelog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
	$reply = pdo_get('jy_vote_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$rid));
	$isdownsao=$this->session('isdownsao');
	$data=array();
	if($reply['vote_codetype']){
		if($reply['vote_codetype']==1 && $isdownsao){
			$data['vote_code']='SN'.get_ordersn('jy_vote_votelog','vote_code');
		}
		if($reply['vote_codetype']==2 && !$isdownsao){
			$data['vote_code']='SN'.get_ordersn('jy_vote_votelog','vote_code');
		}
		if($reply['vote_codetype']==3){
			$data['vote_code']='SN'.get_ordersn('jy_vote_votelog','vote_code');
		}
	}
	if($data['vote_code']){
		pdo_update('jy_vote_votelog',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
	}else{
		exi('领取失败！活动不支持领取！','error');	
	}
	exi($data);
}
if($_RQ['op']=='sendhb'){
	$id=$_RQ['id'];
	if(!$_RQ['id']){
		$id=$this->session('id');
	}
	if(!$id){
		exi('未正确获取作品id','error');
	}
	
	$post=array();
	$post['money']=0;
	 $user=pdo_get('jy_vote_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	 $opus=pdo_get('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($_RQ['postdata']){
	   $postdata=get_postdata();
	   $money=$postdata['money'];
	   $username=$postdata['username'];
		if($money<=1){
		   exi('红包金额需大于1元','error');
	   }
	   if($money>200){
		   exi('单个红包金额最多不超过200元','error');
	   }
	   if(!$username){
		  // exi('请输入发送人姓名！','error');
	   }
	   //创建红包订单
	   $insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'oid'=>$id,
			'omid'=>$opus['mid'],
			'fee'=>$money,
			'ordersn'=>get_ordersn('jy_vote_redorder','ordersn'),
			'username'=>$username,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP
	   );
	   pdo_insert('jy_vote_redorder',$insert);
	   //支付
	   $id=pdo_insertid();
	   $params['fee']=$money;
	    $params['title']='发送红包订单';
		$params['id']=$id;
		$params['plugin']='jy_vote';
		$params['pay_type']='wechat';//可选，此项作用为指定支付方式
		$params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
		$params['back_url']='jy_vote/o_detail/payresult/';
		$return['payid']=cfc('pay')->create_pay_order($params);
	}
	$return['post']=$post;
	exi($return);
}

if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_vote_redorder',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！发送红包订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！发送红包订单金额不匹配！','error');
	}
	pdo_update('jy_vote_redorder',array('ispay'=>1,'fee'=>$payresult['fee']),array('id'=>$order['id']));
	//更新发红包额
	pdo_update('jy_vote_user',array('money_pay +='=>$order['fee']),array('mid'=>$_SESSION['uid']));
	//发红包
	//收红包人的信息
	$suser=pdo_get('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['omid']));
	//发送红包
	
	if($basic['isconfirm']){
		exi('发送成功');
	}
	$nickname = pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'nickname');
	$arr['openid'] = $suser['openid'];
	$arr['hbname'] = '场景投票';
	$arr['body'] = "场景投票红包";
	$arr['fee'] = $order['fee'];
	$arr['mchid'] = $basic['mchid'];
	$arr['mch_appid'] = $basic['appid'];
	$arr['username'] = $order['username']?$order['username']:$nickname;
	$res = sendhongbaoto($arr);
 	if($res['result_code'] == 'SUCCESS' && $res['return_code'] == 'SUCCESS'){
		pdo_update('jy_vote_redorder',array('status'=>1),array('id'=>$order['id']));
		pdo_update('jy_vote_opus',array('money +='=>$order['fee'],'red_num +='=>1),array('id'=>$order['oid']));
		//更新收红包额
		pdo_update('jy_vote_user',array('money_get +='=>$order['fee']),array('mid'=>$order['omid']));
		//发送消息
		$newmember = pdo_get('jy_vote_user',array('id'=>$_SESSION['uid']),'nickname');
		$msg =array('username'=>$newmember['nickname']);
		//set_sms($newmember['mobile'],$msg,1,$_SESSION['uid']);
		$basic['getredtext']= $basic['getredtext']?$basic['getredtext']:'您收到一个好友的鼓励红包';
		set_websms($basic['getredtext'],0,$order['omid'],'');
		//发送wokerman
		cfc('jinyun_socket')->send2uid($basic['getredtext'],$order['omid']);//发送给指定用户
		//发送模板消息
		$this->notice('setred',$_SESSION['uid'],$order['o_id'],$order['id']);
		exi('红包转发成功','success');
	}else{
		pdo_update('jy_vote_user',array('cash +='=>$order['fee']),array('mid'=>$order['omid']));
		//更新发红包额
		pdo_update('jy_vote_user',array('money_pay +='=>$order['fee']),array('mid'=>$_SESSION['uid']));
		exi('红包发送失败','error');
	}
}

if($_RQ['op']=='chouhb'){
	$id=$_RQ['id'];
	if(!$_RQ['id']){
		$id=$this->session('id');
	}
	if(!$id){
		exi('未正确获取作品id','error');
	}
	$opus=pdo_get('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$reply=pdo_get('jy_vote_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$opus['rid']));
	if($reply['endtime']<time()){
			exi('该活动已结束！','error');
	}
	if($reply['starttime']>time()){
			exi('该活动还没开始！','error');
	}
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	//每人每日最多获得红包数量
	$userget=pdo_count('jy_vote_hblog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1,'createtime >'=>$daytime),'id');
	if($userget>=$reply['dairyuserhb']){
		exi('今日已达到获得红包上限，明天再来吧！','error');
	}
	//每日红包数量
	$dayhbnum=pdo_count('jy_vote_hblog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$id,'status'=>1,'createtime >'=>$daytime),'id');
	if($dayhbnum>=$reply['dairyhb']){
		exi('今日已达到获得红包上限，明天再来吧！','error');
	}
	//每日红包数量
	$allhbnum=pdo_count('jy_vote_hblog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$id,'status'=>1),'id');
	if($allhbnum>=$reply['hb_sum']){
		exi('红包已经抽完！下次活动再来吧！','error');
	}	
	//生成一个随机数，判断是否会获得红包
	$rand=rand(1,100) ;
	if($rand>$reply['hb_rate']){
		$ishb=2;
	}else{
		$ishb=1;
	}
	$ok=0;
	//得红包
	if($ishb==1){
		$fee=rand($reply['minhb']*100,$reply['maxhb']*100)/100;//金额
		$ok=1;
		$openid=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'openid');
		$params=array();
		$params['mch_billno'] = $this->config['mch_id'].date("Ymd",time()).date("His",time()).rand(1111,9999);
		$params['openid'] = $openid;
		$params['money'] = $fee;
		$params['remark'] = $reply['hb_remark'];
		$params['wishing'] = $reply['hb_msg'];
		$params['act_name'] = $reply['title'];
		$params['send_name'] = pdo_getcolumn('core_members',array('id'=>$opus['mid'],'uniacid'=>$_SESSION['uniacid']),'nickname');
		$res=cfc('wechat_pay')->redpack($params);
		if($res['result_code'] == 'SUCCESS'){
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'oid'=>$opus['id'],
				'omid'=>$opus['mid'],
				'status'=>1,
				'fee'=>$fee,
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_vote_hblog',$data);
			$return['fee']=$fee;
			$return['ok']=$ok;
		}else{
			exi('红包发送失败','error');
		}
	}else{
		exi('很遗憾没能抽到红包！','error');
	}
	exi($return);
	
}
if($_RQ['op']=='sendlp'){
	$id=$_RQ['id'];
	if(!$_RQ['id']){
		$id=$this->session('id');
	}
	if(!$id){
		exi('未正确获取作品id','error');
	}
	$postdata=get_postdata();
	$lpid=$postdata['lpid'];
	if(!$lpid){
		exi('未获取到礼品信息','error');
	}
	//exi($lpid,'error');
	$opus=pdo_get('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$reply=pdo_get('jy_vote_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$opus['rid']));
	if($reply['endtime']<time()){
			exi('该活动已结束！','error');
	}
	if($reply['starttime']>time()){
			exi('该活动还没开始！','error');
	}
	//查询礼品
	$liping=pdo_get('jy_vote_liping',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lpid));
	//礼品订单
	   $insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'oid'=>$id,
			'lpid'=>$lpid,
			'fee'=>$liping['price'],
			'ordersn'=>get_ordersn('jy_vote_sendlplog','ordersn'),
			'jifen'=>$liping['jifen'],
			'status'=>0,
			'createtime'=>TIMESTAMP
	   );
	   pdo_insert('jy_vote_sendlplog',$insert);
	   //支付
	   $id=pdo_insertid();
	   $params['fee']=$liping['price'];
	    $params['title']='赠送礼品订单';
		$params['id']=$id;
		$params['plugin']='jy_vote';
		$params['pay_type']='wechat';//可选，此项作用为指定支付方式
		$params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
		$params['back_url']='jy_vote/o_detail/lppayresult/';
		$return['payid']=cfc('pay')->create_pay_order($params);
		exi($return);

}
if($_RQ['op']=='lppayresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_vote_sendlplog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！赠送礼品订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！赠送礼品订单金额不匹配！','error');
	}
	//作品
	$opus=pdo_get('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['oid']));
	//活动
	$reply=pdo_get('jy_vote_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$opus['rid']));
	pdo_update('jy_vote_sendlplog',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['id']));
	//积分奖励：
	//送礼赠送(给送礼者)
	if($reply['songlizs']){
		cfc('core')->member_credit($_SESSION['uid'],'credit1',$reply['songlizs'],1,'jy_vote','送礼赠送(给送礼者)');
	}
	//送礼赠送(给参赛者)
	if($reply['songlijl']){
		cfc('core')->member_credit($opus['mid'],'credit1',$reply['songlijl'],1,'jy_vote','送礼赠送(给参赛者)');
	}
	exi('赠送成功');
}
if($_RQ['op']=='getmsg'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_vote');
    $orderby='createtime desc';
	$total='';
	if(isset($_RQ['aid'])){
		$params['aid']=intval($_RQ['aid']);
		$return['aid']=$_RQ['aid'];
	}
	$return['list']=pdo_getslice('core_account_message',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$v){
		$v['member']=cfc('core')->member_show($v['uid']);
		$v['createtime']=date('Y-m-d H:m:s',$v['createtime']);
	}
	exi($return);
}